package com.spd.shop.controller;


import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.spd.shop.common.Result;
import com.spd.shop.mapper.LandlordMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;
import java.util.stream.Collectors;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;

import com.spd.shop.service.ILandlordService;
import com.spd.shop.entity.Landlord;

import org.springframework.web.bind.annotation.RestController;

/**
 * <p>
 * 前端控制器
 * </p>
 *
 * @author 孙沛栋
 * @since 2023-03-17
 */
@RestController
@RequestMapping("/landlord")
public class LandlordController {

    @Resource
    private ILandlordService landlordService;


    //新增或者更新
    @PostMapping
    public boolean save(@RequestBody Landlord landlord) {
        return landlordService.saveOrUpdate(landlord);
    }

    //删除
    @DeleteMapping("/{id}")
    public Boolean delete(@PathVariable Integer id) {
        return landlordService.removeById(id);
    }

    //批量删除
    @PostMapping("/del/batch")
    public boolean deleteBatch(@RequestBody List<Integer> ids) {
        return landlordService.removeBatchByIds(ids);
    }

//    查询所有数据
    @GetMapping
    public List<Landlord> findAll() {
        return landlordService.list();
    }

    //根据id查询
    @GetMapping("/{id}")
    public Landlord findOne(@PathVariable Integer id) {
        return landlordService.getById(id);
    }


    //分页查询
    @GetMapping("/page")
    public Page<Landlord> findPage(@RequestParam Integer pageNum,
                                   @RequestParam Integer pageSize,
                                   @RequestParam(defaultValue = "") String landlordNum,
                                   @RequestParam(defaultValue = "") String landlordName,
                                   @RequestParam(defaultValue = "") String phone) {

        QueryWrapper<Landlord> queryWrapper = new QueryWrapper<>();
        queryWrapper.orderByDesc("id");
        if (!"".equals(landlordNum)) {
            queryWrapper.like("landlord_num", landlordNum);
        }
        if (!"".equals(landlordName)) {
            queryWrapper.like("landlord_name", landlordName);
        }
        if (!"".equals(phone)) {
            queryWrapper.like("phone", phone);

        }
        return landlordService.page(new Page<>(pageNum, pageSize), queryWrapper);
    }


}

